package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Ricard Tang
 * @Date: 2022/10/15/17:35
 * @Description:
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {
    //localhost:8201/swagger-ui.html
    @Resource
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "查询医院设置所有信息")
    @GetMapping("findAll")
    public Result findAll(){
        return Result.ok(hospitalSetService.list());
    }

    @ApiOperation(value = "根据ID删除")
    @DeleteMapping("{id}")
    public Result removeHospitalSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag)
            return Result.ok();
        else
            return Result.fail();
    }

    /**
     * 分页 + 条件查询
     * @param current
     * @param limit
     * @param hospitalSetQueryVo
     * @return
     */
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable Long current ,
                                  @PathVariable Long limit ,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if (hospitalSetQueryVo != null){
            String hosname = hospitalSetQueryVo.getHosname();//医院名称
            String hoscode = hospitalSetQueryVo.getHoscode();//医院编号
            if(!StringUtils.isEmpty(hosname)) {
                wrapper.like("hosname",hospitalSetQueryVo.getHosname());
            }
            if(!StringUtils.isEmpty(hoscode)) {
                wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
            }
        }
        //调用方法实现分页查询
        Page<HospitalSet> info = hospitalSetService.page(page, wrapper);
        //返回结果
        return Result.ok(info);
    }

    /**
     * 新增医院设置
     * @param hospitalSet
     * @return
     */
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        Boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 根据ID查询HospSet
     * @param id
     * @return
     */
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    /**
     * 修改HospSet
     * @param hospitalSet
     * @return
     */
    @PutMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        Boolean flag = hospitalSetService.updateById(hospitalSet);
        if (!flag)
            return Result.fail();
        return Result.ok(flag);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<String> ids){
        hospitalSetService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 医院设置锁定和解锁
     * @param id
     * @param status
     * @return
     */
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(
            @PathVariable Long id,
            @PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    /**
     * 发送签名密钥
     * @param id
     * @return
     */
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODD 发送短信
        return Result.ok();
    }

}
